<?php
/**
	----------------------------------------------
				深圳九企科技
				www.9qi.cn
	----------------------------------------------
	
	优惠券管理
	@author xiaoming <maixiaoming@9qi.cn>
*/
class CouponAction extends BaseAction 
{
	
    /**
		定义空操作
	*/
	public function _empty()
	{
		
		$this->_pageNotFound();
	}
	
	/**
		优惠券列表
	*/
	public function index()
	{

		import("ORG.Util.Page"); // 导入分页类
		$Coupon = M('coupon');
		
		$condition = "";
		$count = $Coupon->where($condition)->count();
		$Page = new Page($count,10);
		$show = $Page->show(); // 分页显示输出
		
		$result = $Coupon->where($condition)
						 ->order('c_id DESC')
						 ->limit($Page->firstRow.','.$Page->listRows)->select();
						 
		if ($result) {
			foreach ($result as &$c) {
				$c['editUrl'] = U("Admin://coupon/edit?id={$c['c_id']}");
				$c['deleteUrl'] = U("Admin://coupon/delete?id={$c['c_id']}");
			}
		}
		
		//獲取分類列表
		$Cat = M('coupon_cat');
		$cat_list = $Cat->select();
		$cats = array();
		foreach($cat_list as $k=>$v) {
			$cats[$v['id']] = $v['name'];
		}

		$this->assign('cats',$cats);

		$addUrl = U("Admin://coupon/add");
		$this->assign('addUrl',$addUrl);
		$this->assign('title','Coupon_list');
		$this->assign('coupons',$result);
		$this->assign('nav', 'coupon');
		$this->display();
	}
	
	/**
		增加优惠券
	*/
	public function add()
	{
		if($_POST['submitted']) {
		
			$Coupon = M('coupon');
			$Coupon->create();
			
			if(!empty($_FILES['photo']['name'])){
				//上传图片处理
				import("ORG.Net.UploadFile");
				
				$upload = new UploadFile(); // 实例化上传类   
				$upload->maxSize  = 3145728 ; // 设置附件上传大小   
				$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型   
				$upload->savePath =  '../Public/Uploads/'; // 设置附件上传目录
				
				if(!$upload->upload()) { // 上传错误 提示错误信息   
					$this->error($upload->getErrorMsg());   
				} else { // 上传成功 获取上传文件信息   
					$info =  $upload->getUploadFileInfo();   
					$Coupon->image_name = $info[0]["savename"];
					$Coupon->image_path = '/Public/Uploads/';
				}  
			}

			$Coupon->create_time = time();
			if($Coupon->add()) {
				echo "<script>alert('添加成功！');</script>";
				$this->redirect('index');
			} else {
				echo "<script>alert('Error!');</script>";
				echo "<script>history.go(-1);</script>";
				exit();
			}
			
		} else {
			//獲取分類列表
			$Cat = M('coupon_cat');
			$cats = $Cat->select();
			$this->assign('cats',$cats);
			
			//显示增加优惠券操作页面
			$this->assign('title','add a coupon');
			$this->assign('nav', 'coupon');
			$this->display();
		}
			
	}
	
	/**
		编辑优惠券
	*/
	public function edit() 
	{
		$Coupon = M('coupon');
		$id = intval($_GET['id']);
		if($_POST['submitted']) {
			
			$Coupon->create();
			if(!empty($_FILES['photo']['name'])){
				//上传图片处理
				import("ORG.Net.UploadFile");
				
				$upload = new UploadFile(); // 实例化上传类   
				$upload->maxSize  = 3145728 ; // 设置附件上传大小   
				$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型   
				$upload->savePath =  '../Public/Uploads/'; // 设置附件上传目录
				
				if(!$upload->upload()) { // 上传错误 提示错误信息   
					$this->error($upload->getErrorMsg());   
				} else { // 上传成功 获取上传文件信息   
					$info =  $upload->getUploadFileInfo();
					$Coupon->image_name = $info[0]["savename"];
					$Coupon->image_path = '/Public/Uploads/';					
				}  
			}

				$Coupon->save();
				echo "<script>alert('修改成功！');</script>";
				$this->redirect("coupon/edit", array('id'=>$_POST['c_id']));
	
		} else {
			$result = $Coupon->where("c_id=$id")->find();
			if ($result) {
				//獲取分類列表
				$Cat = M('coupon_cat');
				$cats = $Cat->select();
				$this->assign('cats',$cats);
			
				$result['img'] = __ROOT__ . $result['image_path'].$result['image_name'];
				$this->assign('coupon',$result);
				$this->assign('c_cat',$result['cat']);
				$this->assign('nav', 'coupon');
				
				$this->display();
			} else {
				$url = 'index';
				$this->redirect($url, '', 5, 'Page Not Found!');
			}
		}
	}
	
	/**
		删除优惠券
	*/
	public function delete()
	{
		$Coupon = M('coupon');
		$id = intval($_GET['id']);
		$result = $Coupon->where("c_id=$id")->delete();
		if($result) {
			$this->redirect("coupon/index");
		} else {
			$this->redirect("coupon/index",array(),3, '删除失败');
		}
	}
}